package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.QianLianYPOrderDao;
import com.cjzuji.rental.dao.WsErYpOrderDao;
import com.cjzuji.rental.mapper.OrderContractMapper;
import com.cjzuji.rental.mapper.OrderMapper;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class WsErYpOrderDaoImpl implements WsErYpOrderDao {
    private static final Logger logger = LoggerFactory.getLogger(WsErYpOrderDaoImpl.class);
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderUserMapper orderUserMapper;
    @Autowired
    OrderContractMapper orderContractMapper;
    @Override
    @Transactional
    public  Long save(Map<String, Object> data, Map<String,Object> detail, List<String> resUser) {
        int idCardStatus = 0;
        if (data.containsKey("idAuthStatus")) {
            if (data.get("idAuthStatus").toString().equals("3")) {
                idCardStatus = 1;
            }
        }
        int orderStatus;
        String deliveryNo =  "";
        switch (data.get("status").toString()) {
            case "1":
                orderStatus = 1;
                break;
            case "5":
                orderStatus = 2;
                break;
            case "15":
                if (Integer.parseInt(data.get("overdueDays").toString()) > 0){
                    orderStatus = 4;  //逾期
                }else {
                    orderStatus = 3;
                }
                deliveryNo = data.get("orderNo").toString();
                break;
            case "55":
                orderStatus = 0;
                break;
            case "220":   //已完成
                orderStatus = 5;
                deliveryNo = data.get("orderNo").toString();
                break;
            case "210":   //已归还
                orderStatus = 5;
                deliveryNo = data.get("orderNo").toString();
                break;
            default:
                orderStatus = 7;
                break;
        }
        Map<String,Object> orderRemarkLog = (Map<String, Object>) data.get("orderRemarkLog");
        String operationName = "";
        if (orderRemarkLog != null){
            operationName = orderRemarkLog.get("operationName").toString();
        }
        double screenRisksAmount = 0;
        int risksType = 0;
        if (!detail.get("realPayScreenRisksAmount").toString().equals("0")){
            screenRisksAmount = (double)detail.get("realPayScreenRisksAmount");
            risksType = 1;
        }
        double discountAmount = 0.0;
        List<Map<String,Object>> couponList = (List<Map<String, Object>>) detail.get("couponList");
        //logger.info("优惠券" + couponList);
        if (couponList.size() > 0 && couponList!=null && couponList.get(0).get("writeOffAmt") != null ){
            //logger.info("优惠券" + couponList.get(0).get("writeOffAmt"));
            discountAmount = Double.parseDouble(couponList.get(0).get("writeOffAmt").toString());
        }
        Orders order = Orders.builder()
                .shopId(3)
                .platformId(10)
                .orderId(data.get("no").toString())
                .opinion(data.get("merchantArtificialOpinion").toString())  //风控意见
                .assigner(operationName)  //分配人 备注人
                .orderIdTag(data.get("id").toString()) //平台订单ID
                .carBrand(data.get("carBrand").toString()) //产品备注
                .colour(data.get("colour").toString())  //商品属性
                .sku(data.get("size").toString())  //商品SKU
                .actualFiancingAmount((double)data.get("actualFiancingAmount"))  //总租金 actualFiancingAmount
                .buyoutAmount((double)data.get("buyoutAmount"))  //买断价 buyoutAmount
                .discountAmount(discountAmount)   //优惠金额
                .monthRepayment((double)data.get("renewMonthRepayment"))  //月租金
                .bondFreeCreditAmt((double)detail.get("totalFinancingAmt") - discountAmount) //订单总金额 frozenAmount
                .surplusBondAmt((double)data.get("surplusBondAmt")) //押金
                .leaseTerm(data.get("termAndRenewTerm").toString())   //订单租期
                .idAuthStatus(idCardStatus)  //身份证上传状态
                .rentStartTime(data.get("rentStartDate").toString())  //租赁开始时间
                .rentLastTime(data.get("rentEndDate").toString())  //租赁结束时间
                .status(orderStatus)    //订单状态
                .deliveryNo(deliveryNo)  //快递单号
                .createTime(data.get("createTime").toString())  // 下单时间
                .updateTime(new Date())
                .screenRisksAmount(screenRisksAmount) //碎屏金额   真实支付的碎屏价格
                .screenRisksType(risksType)  //碎屏状态
                .realName(resUser.get(0))  //下单用户
                .phone(resUser.get(3))  //下单用户手机
                .build();

        try {
            orderMapper.insert(order);
            return  order.getId();//跟这个写的
        }catch (Exception e){
            logger.info("添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }

}
